package org.utils.util;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class CollectionUtils
{
	private static String newLine = System.getProperty("line.separator");

	public static void writeToWriter(Collection s, Writer out)
		throws IOException
	{
		writeToWriter(s.iterator(), out);
	}

	public static void writeToWriter(List l, Writer out)
		throws IOException
	{
		writeToWriter(l.iterator(), out);
	}

	public static void writeToWriter(Enumeration enumList, Writer out)
		throws IOException
	{
		while ( enumList.hasMoreElements() )
		{
			out.write( enumList.nextElement().toString() );
			out.write( newLine );
		}
		out.flush();
	}

	public static void writeToWriter(Iterator it, Writer out) throws IOException
	{
		while ( it.hasNext() )
		{
			out.write( it.next().toString() );
			out.write( newLine );
		}
		out.flush();
	}

	public static void writeToWriter(Map m, Writer out)
		throws IOException
	{
		writeToWriter(m.entrySet().iterator(), out);
	}
	
	public static void arrayToCollection(Collection c, Object[] array)
	{
		for (int i = 0; i < array.length; i++)
			c.add(array[i]);
	}
	
	public static Vector arrayToVector(Object[] array)
	{
		Vector data = new Vector();
		arrayToCollection(data, array);
		return data;
	}
	
	public static ArrayList arrayToArrayList(Object[] array)
	{
		ArrayList data = new ArrayList();
		arrayToCollection(data, array);
		return data;
	}
}
